﻿(function ($) {
    Type.registerNamespace("Carbon");

    Carbon.Popup = function () {
        Carbon.Popup.initializeBase(this);

        this._clientId = null;
        this._transferId = null;
        this._postbackId = null;
        this._serializationType = null;
        this._width = null;
        this._height = null;
        this._async = null;
        this._fetchFromParent = null;
    }

    var prot = Carbon.Popup.prototype;

    var Popup = Carbon.Popup;

    Popup.Factories = {
        OpenPopup: function (ctx) {
            throw new "Unimplemented open popup";
        },
        ParentDocument: function () {
            throw new "Unimplemented parent document";
        },
        ClosePopup: function (invokePostback) {
            throw new "Unimplemented close popup";
        }
    };

    Carbon.Popup.RetrieveData = function (transferClientId, thisClientId) {
        var parentDocument = Popup.Factories.ParentDocument();

        var originalField = parentDocument.getElementById(transferClientId);
        var storageField = document.getElementById(thisClientId);

        if (originalField == null || typeof (originalField) == 'undefined')
            throw "original field doesn't exist";

        if (storageField == null || typeof (storageField) == 'undefined')
            throw "storage field doesn't exist";

        storageField.value = originalField.value;

        __doPostBack("", null);
    };

    prot._PrepareContext = function (ctx) {
        var ctx = $.extend(ctx, { Url: this._url });
        var url = ctx.Url;
        var postbackId = this._postbackId;
        var serializationType = this._serializationType;

        ctx.Popup = this;

        if (url.indexOf('?', 0) < 0) {
            url = url + "?TimeMark=" + new Date().getTime().toString();
        }
        else {
            if (url.indexOf('?', 0) === url.length - 1) {
                url = url + "TimeMark=" + new Date().getTime().toString();
            }
            else {
                url = url + "&TimeMark=" + new Date().getTime().toString();
            }
        }

        ctx.Url = url + "&soid=" + this._transferId + "&FetchFromParent=" + this._fetchFromParent + "&TransferStyle=" + this._transferStyle;

        ctx.Popup = this;

        if (!this._async && this._transferStyle == 1) {
            ctx.Url = ctx.Url + "&PersistanceIdentifier=" + document.getElementById(ctx.Popup._transferId).value;
        }

        if (typeof (ctx.Width) == "undefined") {
            ctx.Width = this._width;
        }
        if (typeof (ctx.Height) == "undefined") {
            ctx.Height = this._height;
        }
        if (typeof (ctx.Async) == "undefined") {
            ctx.Async = this._async;
        }
        if (typeof (ctx.FetchFromParent) == "undefined") {
            ctx.FetchFromParent = this._fetchFromParent;
        }

        var popup = this;

        ctx.InvokeViewModelChanged = function (arg) {
            __doPostBack(ctx.Popup._postbackId, "ViewModelChanged");
        };
    };

    prot.OpenPopup = function (pCtx) {
        if (!pCtx) {
            pCtx = {};
        }

        this._PrepareContext(pCtx);

        if (pCtx.Async && pCtx.FetchFromParent) {
            __doPostBack(pCtx.Popup._postbackId, "Fetch");
        }
        else {
            Popup.Factories.OpenPopup(pCtx);
        }
    };

    Carbon.Popup.AsyncOpen = function (popup) {
        var ctx = {};

        popup._PrepareContext(ctx);

        if (ctx.Popup._transferStyle == 1) {
            ctx.Url = ctx.Url + "&PersistanceIdentifier=" + document.getElementById(ctx.Popup._transferId).value;
        }

        Popup.Factories.OpenPopup(ctx);
    };

    Popup.ClosePopup = function () {
        Popup.Factories.ClosePopup(false);
    };

    Popup.ReturnData = function (transferClientId, thisClientId) {
        var parentDocument = Popup.Factories.ParentDocument();

        var originalField = parentDocument.getElementById(transferClientId);
        var storageField = document.getElementById(thisClientId);

        if (originalField == null || typeof (originalField) == 'undefined')
            throw "original field doesn't exist";

        if (storageField == null || typeof (storageField) == 'undefined')
            throw "storage field doesn't exist";

        originalField.value = storageField.value;

        Popup.Factories.ClosePopup(true);
    };

    prot.InitializePopup = function (clientId, postbackId, transferId, url, width, height, async, fetchFromParent, invokeOpen, transferStyle) {
        this._clientId = clientId;
        this._postbackId = postbackId;
        this._transferId = transferId;
        this._width = width;
        this._height = height;
        this._async = async;
        this._fetchFromParent = fetchFromParent;
        this._transferStyle = transferStyle;
        this._url = url;

        if (invokeOpen) {
            var This = this;
            setTimeout(function () {
                Carbon.Popup.AsyncOpen(This);
            }, 0);
        }
    };

    Popup.registerClass('Carbon.Popup', Sys.Component);

    if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
})(jQuery);